/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'guarantee_item_list',params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>消费者保障服务管理</h3>
            <h5>消费者保障服务查看与管理</h5>
          </div>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="项目名称：" required>
          <el-input v-model="form.grt_name"></el-input>
          <p class="notic">项目名称不能为空且不能大于50个字符</p>
          <p class="err-msg" v-if="errResult.grt_name && !form.grt_name">{{errResult.grt_name}}</p>
        </el-form-item>
        <el-form-item label="保证金：" required>
          <el-input v-model="form.grt_cost"></el-input>
          <p class="notic">保证金不能为空且必须为数字</p>
        </el-form-item>
        <el-form-item label="项目图标：" required>
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=icon'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-image v-if="form.grt_icon" :src="form.grt_icon" style="width: 50px; height: 50px"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="err-msg" v-if="errResult.grt_icon && !form.grt_icon">{{errResult.grt_icon}}</p>
        </el-form-item>
        <el-form-item label="项目描述：" required>
          <el-input
            type="textarea"
            v-model="form.grt_describe"
            style="width:450px;"
            :autosize="{ minRows: 2, maxRows: 4}"
          ></el-input>
          <p class="notic">项目描述不能为空且小于200个字符</p>
          <p
            class="err-msg"
            v-if="errResult.grt_describe && !form.grt_describe"
          >{{errResult.grt_describe}}</p>
        </el-form-item>
        <el-form-item label="说明文章链接地址：" required>
          <el-input v-model="form.grt_descurl"></el-input>
        </el-form-item>
        <el-form-item label="排序：" required>
          <el-input v-model="form.grt_sort"></el-input>
          <p class="notic">排序应为大于1的正整数</p>
        </el-form-item>
        <el-form-item label="状态：">
          <el-switch
            v-model="form.grt_state"
            :active-value="1"
            :inactive-value="0"
            active-text="开启"
            inactive-text="关闭"
          ></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="loading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  getGuaranteeItem,
  updateGuaranteeItem,
  addGuaranteeItem
} from "@/utils/api";
export default {
  data() {
    return {
      form: {},
      goods_category_tree: [],
      category: [],
      loading: false,
      errResult: {}
    };
  },
  created() {
    if (this.$route.params.id) {
      this.getGuaranteeItem();
    }
  },
  methods: {
    getGuaranteeItem() {
      var that = this;
      getGuaranteeItem({
        grt_id: this.$route.params.id
      }).then(function(res) {
        that.form = res;
      });
    },
    handleAvatarSuccess(res) {
      this.form.grt_icon = res.result;
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isPic =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPic) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isPic && isLt2M;
    },
    onSubmit() {
      this.loading = true;
      var that = this;
      if (this.form.grt_id) {
        //编辑
        updateGuaranteeItem(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message({
              message: "保存成功",
              type: "success",
              onClose: function() {
                that.$router.push({ name: "guarantee_item_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.loading = false;
        });
      } else {
        //添加
        addGuaranteeItem(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message({
              message: "添加成功",
              type: "success",
              onClose: function() {
                that.$router.push({ name: "guarantee_item_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.loading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
</style>
